package com.demo.utils;

/**
 * author:王蒙蒙
 * date创建日期:2025/7/25
 * 类注释说明:测试结果历史记录工具类
 **/

import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class TestResultHistory {
    private static final String HISTORY_FILE = "test_result_history.txt";
    private static final Logger log = LogUtils.getLogger();

    /**
     * 保存本次测试结果
     */
    public static void saveCurrentResult(int total, int passed, int failed, int errors, int skipped) {
        String content = String.format(
                "测试用例总数: %d\n通过率: %.2f%%\n成功数: %d\n失败数: %d\n错误数: %d\n跳过数: %d",
                total, (passed * 100.0 / total), passed, failed, errors, skipped
        );
        try {
            Files.write(Paths.get(HISTORY_FILE), content.getBytes());
        } catch (IOException e) {
            log.error("保存测试结果失败: " + e.getMessage());
        }
    }

    /**
     * 读取上一次测试结果
     */
    public static String readLastResult() {
        try {
            return new String(Files.readAllBytes(Paths.get(HISTORY_FILE)));
        } catch (IOException e) {
            return "无历史记录";
        }
    }
}